package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.bo.ChannelDataBo;
import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.ChannelData;
import cn.broadsense.biz.service.ChannelDataService;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.enums.BusinessType;
import cn.broadsense.common.utils.SecurityUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author lijian
 * @description
 * @date 2024年09月23日 下午4:48
 */
@RestController
@Api(tags = "数据点位")
@RequestMapping("/channel/data")
@RequiredArgsConstructor
public class ChannelDataController extends BaseController {

    private final ChannelService channelService;

    private final ChannelDataService channelDataService;

    @ApiOperation(value = "保存数据点位管理")
    @Log(title = "数据点位-保存数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@Validated @RequestBody ChannelDataBo bo) {
        Channel channel = channelService.lambdaQuery().select(Channel::getId).eq(Channel::getId, bo.getChannelId()).last("limit 1").one();
        if (channel == null) {
            return R.fail("所选点位不存在");
        }
        ChannelData data = BeanUtil.copyProperties(bo, ChannelData.class);
        channelDataService.save(data);
        return R.ok();
    }

    @ApiOperation(value = "数据治理")
    @Log(title = "数据点位-数据治理", businessType = BusinessType.UPDATE)
    @PutMapping("/govern/{id}")
    public R govern(@Validated @RequestBody ChannelDataBo bo, @PathVariable Long id) {
        ChannelData channelData = channelDataService.lambdaQuery().select(ChannelData::getId).eq(ChannelData::getId,id).last("limit 1").one();
        if(channelData == null){
            return R.ok("所选点位不存在");
        }
        ChannelData data = BeanUtil.copyProperties(bo, ChannelData.class);
        data.setId(id);
        data.setGovernBy(SecurityUtils.getUsername());
        data.setGovernTime(new DateTime());
        data.setGovernStatus(1);
        channelDataService.updateById(data);
        return R.ok();
    }

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public TableDataInfoResult<List<ChannelData>> list(@ApiParam(value = "设备名称") @RequestParam(required = false) String deviceName,
                                                        @ApiParam(value = "是否治理") @RequestParam(required = false) Integer governStatus,
                                                        @RequestParam(required = false) @ApiParam(value = "开始时间 YYYY-MM-DD") String startTime,
                                                        @RequestParam(required = false) @ApiParam(value = "结束时间 YYYY-MM-DD") String endTime) {
        startPage();
        List<ChannelData> list = channelDataService.lambdaQuery()
                .like(StrUtil.isNotBlank(deviceName), ChannelData::getDeviceName, deviceName)
                .eq(Objects.nonNull(governStatus), ChannelData::getGovernStatus, governStatus)
                .between(StrUtil.isNotBlank(startTime) && StrUtil.isNotEmpty(endTime), ChannelData::getGovernTime, startTime, endTime)
                .orderByDesc(ChannelData::getGovernTime)
                .list();
        return getDataTableResult(list);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id){
        ChannelData channelData = channelDataService.lambdaQuery().select(ChannelData::getId).eq(ChannelData::getId,id).last("limit 1").one();
        if(channelData == null){
            return R.ok("点位不存在");
        }
        channelDataService.removeById(id);
        return R.ok();
    }

    @ApiOperation("同步采集数据")
    @PutMapping("/syncData/{ids}")
    public R syncData(@PathVariable Long[] ids){
        channelDataService.syncData(Arrays.asList(ids));
        return R.ok();
    }
}
